import http from '@/api/index';
import { getUUID, isEmpty, getStorage } from '@/assets/js/utils';
import { useTranslateLangStore } from '@/stores/translateLang';

enum API {
    TRANSLATE_TEXT = '/translate/translate/text', //翻译文本
    VOICE_RECOGNIZE_CN = '/translate/asr/recognizeCh', //语音识别中文
    VOICE_RECOGNIZE_FOREIGN = '/translate/asr/recognizeLao', //语音识别外语
    VOICE_RECOGNIZE_FOREIGN_V2 = '/translate/asr/recognizeForeign', //语音识别外语版本2
    GET_TRANSLATE_HISTORY_LIST = '/translate/translate/listHistory', //获取历史翻译记录
    GET_AUDIO_BASE64_BY_ID = '/translate/audio/tts', //通过ID获取语音base64格式音频
    GET_AUDIO_FILE_BY_ID = '/translate/audio/ttsFile', //通过ID获取语音wav格式音频
    DEL_TRANSLATE_HISTORY = '/translate/translate/delHistory', //删除翻译记录
    COLLECT_TRANSLATE = '/translate/translate/collect', //收藏
    GET_COLLECT_LIST = '/translate/translate/listCollection' //获取收藏列表
}

//翻译文本
export function translateText(data: any) {
    return http.post({
        url: API.TRANSLATE_TEXT,
        data,
        errToast: false
    });
}

//语音识别，type: 1中文,2外语
export function voiceRecognize(file: any, type: number = 1, onProgress?: Function): any {
    return new Promise(async (resolve, reject) => {
        let uploadTask = uni.uploadFile({
            url: import.meta.env.VITE_APP_BASEURL + (type == 1 ? API.VOICE_RECOGNIZE_CN : API.VOICE_RECOGNIZE_FOREIGN_V2) + '?modelType=' + useTranslateLangStore().sourceLang,
            filePath: file,
            name: 'file',
            method: 'POST',
            header: {
                'Content-Type': 'multipart/form-data',
                Authorization: getStorage('token'),
                modelType: useTranslateLangStore().sourceLang
            },
            formData: {
                modelType: useTranslateLangStore().sourceLang
            },
            success: (res: any) => {
                if (res.statusCode == '200') {
                    let resultData = JSON.parse(res.data);
                    if (resultData.code == 0) {
                        if (resultData.data) {
                            resolve([resultData.data, null]);
                        } else {
                            resolve([null, '没有听清，请再说一次~']);
                        }
                    } else {
                        resolve([null, resultData.message]);
                    }
                } else {
                    let { error } = JSON.parse(res.data);
                    resolve([null, error]);
                }
            },
            fail: err => {
                resolve([null, err]);
            }
        });
        uploadTask.onProgressUpdate(({ progress }) => {
            if (onProgress) onProgress(progress);
        });
    });
}

//获取历史翻译记录
export function getTranslateHistoryList(data: any) {
    return http.get({
        url: API.GET_TRANSLATE_HISTORY_LIST,
        data,
        errToast: false
    });
}

//通过ID获取语音base64格式音频
export function getAudioBase64ById(id: any) {
    return http.get({
        url: `${API.GET_AUDIO_BASE64_BY_ID}/${id}`
    });
}

//通过ID获取语音wav格式音频
export function getAudioFileById(id: any) {
    return new Promise<any>((resolve, reject) => {
        uni.downloadFile({
            url: `${import.meta.env.VITE_APP_BASEURL}${API.GET_AUDIO_FILE_BY_ID}/${id}`,
            success: res => {
                if (res.statusCode === 200) {
                    // uni.saveFile({
                    //     tempFilePath: res.tempFilePath,
                    //     success: function (res) {
                    //         console.log(JSON.stringify(res));
                    //         resolve([res.savedFilePath, null]);
                    //     }
                    // });
                    resolve([res.tempFilePath, null]);
                } else {
                    resolve([null, res.errMsg]);
                }
            },
            fail: err => {
                resolve([null, err]);
            }
        });
    });
}

//删除翻译记录
export function delTranslateHistory() {
    return http.get({
        url: API.DEL_TRANSLATE_HISTORY
    });
}

//收藏
export function collectTranslate(id: any) {
    return http.get({
        url: `${API.COLLECT_TRANSLATE}/${id}`
    });
}

//获取收藏列表
export function getCollectList(id: any) {
    return http.get({
        url: API.GET_COLLECT_LIST
    });
}
